<template>
  <CustomDialog :drag-dialog="dialogInfo">
    <el-form ref="form" :model="form" :rules="rules" :disabled="!form.busPageInfo.editable" size="mini" label-width="160px">
      <el-form-item label="结算科目" prop="busMainData.finCode">
        <el-input v-model="form.busMainData.finCode" :disabled="!isAdd" />
      </el-form-item>
      <el-form-item label="结算科目名称" prop="busMainData.finName">
        <el-input v-model="form.busMainData.finName" />
      </el-form-item>
      <el-form-item style="margin-bottom: 0px;" label="上级科目" prop="busMainData.pid">
        <FinItemTreeSelect ref="finItemTreeSelect" v-model="form.busMainData.pid" :data-trees="finItems" value-field="id" style="width: 200px;" placeholder="选择上级科目" />
      </el-form-item>
      <el-form-item label="余额方向" prop="busMainData.finBalanceDir">
        <el-radio-group v-model="form.busMainData.finBalanceDir">
          <el-radio v-for="item in dicts.fin_balance_dir" :label="item.dictCode" :key="item.dictCode" >{{ item.dictName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="科目类别" prop="busMainData.finCourceType">
        <el-select v-model="form.busMainData.finCourceType" size="mini" clearable placeholder="请选择">
          <el-option v-for="item in dicts.fin_cource_type" :key="item.id" :label="item.dictName" :value="item.dictCode"/>
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="busMainData.activeFlag">
        <el-radio-group v-model="form.busMainData.activeFlag">
          <el-radio v-for="item in dicts.enable_flag" :label="item.dictCode" :key="item.dictCode" >{{ item.dictName }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="备注" prop="busMainData.busRmk">
        <el-input
          v-model="form.busMainData.rmk"
          size="mini"
          class="filter-item" />
      </el-form-item>
    </el-form>
    <div v-if="form.busPageInfo.editable" slot="footer" class="modal-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button v-permission="['SYS_ADMIN','COMMON_ADMIN','FIN_ITEM_ALL','FIN_ITEM_CREATE']" :loading="loading" type="primary" @click="doSave">保存</el-button>
    </div>
  </CustomDialog>
</template>

<script>
import finItem from '@/api/fin/FinItem'
import FinItemTreeSelect from '@/views/fin/baseitem/module/FinItemTreeSelect'

export default {
  components: { FinItemTreeSelect },
  props: {
    sup_this: {
      type: Object,
      required: true
    }
  },
  dictTypes: ['fin_balance_dir', 'enable_flag', 'fin_cource_type'],
  data() {
    return {
      isAdd: false,
      loading: false,
      style: 'width: 184px',
      finItems: [],
      dialogInfo: {
        name: 'finItemMngFormDialog',
        title: '会计科目维护',
        fullScreen: false,
        width: 500,
        height: 70
      },
      form: {
        busMainData: {
          id: '',
          enableFlag: 'Y'
        },
        busPageInfo: {
          editable: false
        }
      },
      rules: {
        'busMainData.finCode': [
          { required: true, message: '请输入科目', trigger: 'blur' },
          { min: 3, max: 40, message: '长度在 3 到 40 个字符', trigger: 'blur' }
        ],
        'busMainData.finName': [
          { required: true, message: '请输入名称', trigger: 'blur' },
          { min: 3, max: 100, message: '长度在 3 到 100 个字符', trigger: 'blur' }
        ],
        'busMainData.finBalanceDir': [
          { required: true, message: '请输入余额方向', trigger: 'blur' }
        ],
        'busMainData.finCourceType': [
          { required: true, message: '请输入科目类别', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    cancel() {
      this.resetForm()
    },
    initDataOfBefore(id) {
      finItem.getFinItemData({ 'id': id, 'activeFlag': 'Y' }).then(res => {
        this.finItems = res.data
      })
    },
    displayDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.show(name)
      })
    },
    hiddenDialog() {
      const { name } = this.dialogInfo
      this.$nextTick(() => {
        this.$modal.hide(name)
      })
    },
    addData() {
      this.isAdd = true
      const data = { paramBean: {}}
      finItem.toAdd(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    copyData(mainId) {
      this.isAdd = true
      const data = { paramBean: { id: mainId }}
      finItem.toCopy(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        console.log(err)
      })
    },
    viewMainData(id) {
      this.isAdd = false
      const data = { paramBean: { id: id }}
      finItem.viewMainData(data).then(res => {
        this.displayDialog()
        this.form = res.data
      })
    },
    doSubmit() {
      this.doSingleSave(true)
    },
    doSave() {
      this.doSingleSave(false)
    },
    doSingleSave(isSubmit) {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.formSubmit = isSubmit
          this.loading = true
          if (this.isAdd) {
            this.doAdd()
          } else {
            this.doEdit()
          }
        }
      })
    },
    doAdd() {
      finItem.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    doEdit() {
      finItem.addOrEdit(this.form).then(res => {
        this.resetForm()
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err)
      })
    },
    resetForm() {
      this.hiddenDialog()
      this.$refs['form'].resetFields()
    },
    changeData(busId) {
      const data = { paramBean: { id: busId }}
      finItem.toChange(data).then(res => {
        this.displayDialog()
        this.form = res.data
      }).catch(err => {
        this.dialog = false
        console.log(err)
      })
    }
  }
}
</script>
